<template>
  <div class="login">
    <div class="container"
         :class="loginType==='phone'?'':'hover'">
      <div class="page-front">
        <div class="login-type-phone">
          <div class="module-title drag flex justify-between  p-l-10 p-r-10 p-t-10">
            <div class="flex qrcode-tip align-center">
              <BarcodeOutlined />
              <div class="tip">
                扫码登录更安全
              </div>
            </div>
            <CloseOutlined @click="handleClose"
                           class="close no-drag" />
          </div>
          <div class="login-bg p-t-5 ">
            <PhoneOutlined />
          </div>

          <a-form :model="phoneForm"
                  @submit="handlePhoneSubmit"
                  class="myForm">
            <a-form-item>
              <a-input-group compact
                             style="width:100%">
                <a-select style="width:35%"
                          v-model:value="phoneForm.countrycode">
                  <a-select-option value="86">
                    +86
                  </a-select-option>
                  <a-select-option value="1">
                    +1
                  </a-select-option>
                </a-select>
                <a-input style="width:65%;padding-left:15px"
                         placeholder="请输入手机号"
                         v-model:value="phoneForm.phone" />
              </a-input-group>
            </a-form-item>
            <a-form-item>
              <a-input v-model:value="phoneForm.password"
                       type="password"
                       class="flex align-center m-t-10"
                       placeholder="请输入密码">
                <template #prefix>
                  <LockOutlined style="color:rgba(0,0,0,.25);margin-left:10px" />
                </template>
                <template #suffix>
                  <a-button style="height:0px;line-height:0px;margin-top:-17px"
                            type="link">重设密码</a-button>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-checkbox v-model:checked="emailForm.autoLogin">
                自动登录
              </a-checkbox>
            </a-form-item>
            <a-form-item>
              <a-button type="primary"
                        html-type="submit">
                登录
              </a-button>
            </a-form-item>
          </a-form>

          <p class="text-center"><span class="register">注册</span></p>

          <div class="othart-login-icon flex
                                   align-center
                                   justify-between">
            <div class="icon">
              <WechatOutlined @click="handleOtherLogin('weixin')"
                              class="weixin" />
            </div>
            <div class="icon">
              <QqOutlined class="qq"
                          @click="handleOtherLogin('qq')" />
            </div>
            <div class="icon">
              <WeiboOutlined class="weibo"
                             @click="handleOtherLogin('weibo')" />
            </div>
            <div class="icon">
              <GithubOutlined class="email"
                              @click="handleOtherLogin('email')" />
            </div>
          </div>
          <div class="agreement">
            <a-checkbox v-model:checked="agreement">同意
              <span class="text-blue">《服务条款》</span>
              <span class="text-blue">《隐私政策》</span>
            </a-checkbox>
          </div>
        </div>
      </div>
      <div class="page-back">
        <div v-if="loginType==='email'"
             class="login-type-email">
          <div class="module-title drag flex justify-between  p-l-10 p-r-10 p-t-10">
            <span class="no-drag"
                  @click="loginType='phone'">
              <LeftOutlined />
              返回其他登录
            </span>
            <CloseOutlined @click="handleClose"
                           class="close no-drag" />
          </div>
          <div class="login-bg p-t-20 p-b-30">
            <MailOutlined />
          </div>

          <a-form :model="emailForm"
                  @submit="handleEmailSubmit"
                  class="myForm">
            <a-form-item>
              <a-input v-model:value="emailForm.email"
                       placeholder="请输入邮箱">
                <template #prefix>
                  <UserOutlined style="color:rgba(0,0,0,.25);margin-left:10px" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input v-model:value="emailForm.password"
                       type="password"
                       placeholder="请输入密码">
                <template #prefix>
                  <LockOutlined style="color:rgba(0,0,0,.25);margin-left:10px" />
                </template>
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-checkbox v-model:checked="emailForm.autoLogin">
                自动登录
              </a-checkbox>
            </a-form-item>
            <a-form-item>
              <a-button type="primary"
                        html-type="submit">
                登录
              </a-button>
            </a-form-item>
          </a-form>

        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  LeftOutlined, CloseOutlined, MailOutlined, UserOutlined, LockOutlined, BarcodeOutlined, WechatOutlined, QqOutlined,
  WeiboOutlined, GithubOutlined, PhoneOutlined
} from "@ant-design/icons-vue"
import { message } from 'ant-design-vue';
import { ref, getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import { loginByEmail, loginByPhone } from '@/utils/api'
import storage from '@/utils/storage';
const { remote, ipcRenderer } = window.require('electron')
export default {
  components: {
    LeftOutlined,
    CloseOutlined,
    MailOutlined,
    UserOutlined,
    LockOutlined,
    BarcodeOutlined,
    WechatOutlined,
    QqOutlined,
    WeiboOutlined,
    GithubOutlined,
    PhoneOutlined
  },
  setup() {
    const { ctx } = getCurrentInstance()
    const store = useStore()
    const win = remote.getCurrentWindow()
    // win.show()
    const loginType = ref('phone')
    const agreement = ref(false)
    const phoneForm = ref({
      phone: '',
      password: '',
      autoLogin: false,
      countrycode: '86'
    })
    const emailForm = ref({
      email: '',
      password: '',
      autoLogin: false,
    })
    const handleOtherLogin = type => {
      if (!agreement.value) {
        message.error('请先勾选协议!')
        return false
      }
      switch (type) {
        case 'email':
          loginType.value = 'email'
          break;
      }
    }

    const handlePhoneSubmit = e => {
      // ctx.$router.push('/')
      if (!phoneForm.value.phone || !phoneForm.value.password) {
        message.error('请输入手机号、密码');
        return false
      }
      loginByPhone(
        {
          phone: phoneForm.value.phone,
          password: phoneForm.value.password
        }
      ).then(res => {
        console.log('phone登录信息', res);
        if (res.code !== 200) {
          return false
        }
        storage.set('users', res)
        // ctx.$store.commit('setUsers', res)
        store.commit('setUsers', res)
        ipcRenderer.send('login-success', res)
        message.success('登陆成功');
        win.hide()
        // ctx.$router.push('/')
      })
    }
    const handleEmailSubmit = e => {
      if (!emailForm.value.email || !emailForm.value.password) {
        message.error('请输入邮箱、密码');
        return false
      }
      // win.reload()
      loginByEmail({
        ...emailForm.value
      }).then(res => {
        console.log('email登录信息', res);
        if (res.code !== 200) {
          return false
        }
        message.success('登陆成功');
        storage.set('users', res)
        // ctx.$store.commit('setUsers', res)
        store.commit('setUsers', res)
        ipcRenderer.send('login-success', res)
        message.success('登陆成功');
        win.hide()
        // ctx.$router.push('/')
      })
    }
    const handleClose = () => {
      win.close()
    }
    return {
      agreement,
      emailForm,
      phoneForm,
      handlePhoneSubmit,
      handleEmailSubmit,
      handleClose,
      loginType,
      handleOtherLogin
    }
  }
}
</script>

<style lang='scss' scoped >
.login {
  width: 100%;
  height: 100vh;
  padding: 15px;
  overflow: hidden;
  perspective: 1000;
  border-radius: 6px;
  .container {
    width: 100%;
    height: 100%;
    transition: 0.8s;
    transform-style: preserve-3d;
    position: relative;
    background: #fff;
    &.hover {
      transform: rotateY(180deg);
    }
    .page-back,
    .page-front {
      width: 100%;
      height: 100%;
      backface-visibility: hidden; // 设置背面可见性为隐藏
      position: absolute;
      top: 0;
      left: 0;
    }
    .page-front {
      box-shadow: 2px 2px 10px #000;
      z-index: 2; // 卡片的正面zindex 设置的大一点
    }
    .page-back {
      box-shadow: 2px 2px 10px #000;

      transform: rotateY(180deg); // 让背面旋转180deg
    }
  }
  .module-title {
    user-select: none;
    .close {
      font-size: 22px;
      cursor: pointer;
      color: #999;
    }
  }
  .login-bg {
    color: $paimary;
    text-align: center;
    font-size: 90px;
    opacity: 0.7;
  }
  .myForm {
    padding: 0 50px;
    .ant-form-item {
      width: 100%;
      ::v-deep .ant-select-selector {
        height: 37px;
        line-height: 37px;
      }
      ::v-deep input {
        line-height: 35px;
      }
      ::v-deep .ant-col {
        width: 100% !important;
      }
      ::v-deep .ant-form-item-children {
        width: 100% !important;
        .ant-btn {
          width: 100% !important;
          height: 40px;
          line-height: 40px;
          border-radius: 5px;
          margin-top: 10px;
        }
      }
    }
  }
  .login-type-phone {
    .qrcode-tip {
      .anticon {
        font-size: 40px;
      }
      .tip {
        padding: 4px 5px;
        background: rgba(0, 0, 0, 0.4);
        color: #fff;
        margin-left: 4px;
        border-radius: 4px;
        font-size: 11px;
      }
    }
    .register {
      display: inline-block;
      padding-top: 8px;
      border-bottom: 1px solid #999;
      cursor: pointer;
    }
    .othart-login-icon {
      padding: 0 50px;
      font-size: 30px;
      margin-top: 10px;
      .icon {
        width: 40px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        border-radius: 50%;
        border: 1px solid #999;
        .weixin {
          color: #67b633;
        }
        .qq {
          color: #34a0df;
        }
        .weibo {
          color: #ea4848;
        }
        .email {
          color: #da5d56;
        }
      }
    }
    .agreement {
      padding: 0 45px;
      margin-top: 15px;
      .text-blue {
        color: #34a0df;
      }
    }
  }
  .login-type-email {
  }
}
</style>